<template>
  <div>
    <!-- 添加Spu按钮 -->
    <el-button
      type="primary"
      :disabled="!categoryStore.category3Id"
      :icon="Plus"
      @click="addSkuInfo"
      >添加SUP</el-button
    >

    <!-- 表格 -->
    <el-table :data="spuList" border style="margin: 15px 0">
      <el-table-column
        type="index"
        label="序号"
        width="80px"
        align="center"
      ></el-table-column>
      <el-table-column label="SPU名称" prop="spuName"></el-table-column>
      <el-table-column label="SPU描述" prop="description"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <!-- 添加SKU -->
          <el-button
            title="添加SKU"
            :icon="Plus"
            size="small"
            type="primary"
            @click="toAddSkuInfo(row)"
          ></el-button>
          <!-- 修改SPU -->
          <el-button
            title="修改SPU"
            :icon="Edit"
            size="small"
            type="primary"
            @click="toEditSpuInfo(row)"
          ></el-button>
          <!-- 查看SKU列表 -->
          <el-button
            title="查看SKU列表"
            :icon="InfoFilled"
            size="small"
            type="info"
            @click="toCheckSkuList(row.id)"
          ></el-button>
          <!-- 删除SPU -->
          <el-popconfirm
            title="确定要删除此属性值吗?"
            @confirm="deleteSpuInfo(row.id)"
          >
            <template #reference>
              <el-button
                title="删除SPU"
                :icon="Delete"
                size="small"
                type="danger"
              ></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 6, 9, 12]"
      layout=" prev, pager, next, jumper,->, sizes,total"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!-- 查看SKU列表弹窗 -->
    <el-dialog v-model="dialogTableVisible" title="查看SKU数据列表">
      <el-table :data="skuInfo" border>
        <el-table-column prop="skuName" label="名称" width="150" />
        <el-table-column prop="price" label="价格(元)" width="200" />
        <el-table-column prop="weight" label="重量" />
        <el-table-column label="默认图片">
          <template v-slot="{ row }">
            <img
              :src="row.skuDefaultImg"
              :alt="row.skuDefaultImg"
              style="width: 100px; height: 100px"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script lang="ts">
export default {
  name: "SpuList",
}
</script>

<script lang="ts" setup>
import { ref, watch } from "vue"
import { Plus, Edit, Delete, InfoFilled } from "@element-plus/icons-vue"
import { useCategoryListStore } from "@/stores/categoryList"
import { reqGetSpuListData, reqDeleteSpuInfo } from "@/api/product/spu.ts"
import { reqGetSkuListData } from "@/api/product/sku.ts"

import { ElMessage } from "element-plus"
const categoryStore = useCategoryListStore()
const emits = defineEmits(["changeScene", "getSpuInfo"])

const spuList = ref([])

// 分页器相关的变量
const currentPage = ref<number>(1)
const pageSize = ref<number>(3)
const total = ref(0)

const dialogTableVisible = ref(false)

// 存放sku详细信息的变量
const skuInfo = ref([])

// 1.封装调用接口函数的函数
const getSpuListData = async () => {
  const result = await reqGetSpuListData(
    currentPage.value,
    pageSize.value,
    categoryStore.category3Id
  )
  spuList.value = result.records

  total.value = result.total
}

// 2. 监听数据变化 获取数据 页面挂载完成时调用函数，
watch(
  () => categoryStore.category3Id,
  category3Id => {
    if (!category3Id) return (spuList.value = [])
    getSpuListData()
  },
  { immediate: true }
)

// 分页相关的两个事件
// 3. 更改每页显示条数时触发的事件处理程序
const handleSizeChange = (val: number) => {
  // val就是每页显示的条数 其实也相当于是pageSize
  currentPage.value = 1
  pageSize.value = val
  getSpuListData()
}

// 4. 根据当前页码进行切换时触发的函数
const handleCurrentChange = (val: number) => {
  // console.log(`current page: ${val}`)
  currentPage.value = val
  getSpuListData()
}

// 5.删除spuInfo
const deleteSpuInfo = async (id: number) => {
  await reqDeleteSpuInfo(id)
  ElMessage.success("spuInfo删除成功")
  getSpuListData()
}

// 6. 编辑SpuInfo 之数据回显
const toEditSpuInfo = row => {
  // 跳转到SpuForm.vue页面
  emits("changeScene", 2)
  // 传递参数
  emits("getSpuInfo", row)
}

// 7.// 跳转到SkuForm.vue页面
const toAddSkuInfo = row => {
  emits("changeScene", 3)
  emits("getSpuInfo", row)
}

// 8. 添加Spu按钮
const addSkuInfo = () => {
  emits("changeScene", 2)
  emits("getSpuInfo", {})
}

// 9. 查看skuList
const toCheckSkuList = async (id: number) => {
  dialogTableVisible.value = true
  const result = await reqGetSkuListData(id)
  // console.log(result)
  skuInfo.value = result
}
</script>

<style></style>
